package LeetCode.month01.day29;

import java.util.HashMap;

public class No3 {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }
    public static int lengthOfLongestSubstring(String s) {
        int left = 0;
        int n = s.length();
        char[] chars = s.toCharArray();
        int res = 0;
        HashMap<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if(map.get(chars[i])!=null){
                res = Math.max(res,i-left);
                int index = map.get(chars[i]);
                for (int j = left; j <=index ; j++) {
                    map.remove(chars[j]);
                }
                left = index+1;
            }
            map.put(chars[i],i);
        }
        return Math.max(res,n-left);
    }
}
